package com.my.company.user.action;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.my.company.admin.service.ChangePathService;
import com.my.company.admin.service.DeptService;
import com.my.company.admin.service.HonerService;
import com.my.company.user.pojo.ChangePath;
import com.my.company.user.pojo.Dept;
import com.my.company.user.pojo.Honer;
import com.my.company.user.pojo.User;
import com.my.company.user.service.UserService;
import com.my.company.util.MD5;
import com.my.company.util.StringIsValitedUtils;
import com.my.company.util.ValidateUtil;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@Controller("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport implements ServletContextAware {
	
	@Resource
	private UserService us;
	@Resource
	private DeptService deptService;
	@Resource
	private ChangePathService cps;
	private User user;
	private List<User> userList;

	private File picture;
	private String pictureFileName;
	private ServletContext sc;

	private List<Dept> deptList;
	private Dept dept;
	private List<ChangePath> changePathList;
	private String message;
	private HttpServletRequest request = ServletActionContext.getRequest();
	@Resource
	private HonerService hs;
	private List<Honer> honerList;
	private String rePwd;

	public String userRegist() {
		deptList = deptService.findEntityByHQL("from Dept where 1=?", 1);
		return SUCCESS;
	}

	public String showUserHoner() {
		String sql = "from Honer h where h.userId=?";
		user = (User) request.getSession().getAttribute("user");
		honerList = hs.findEntityByHQL(sql, user.getUserId());
		return SUCCESS;
	}

	public String exitUser() {
		request.getSession().setAttribute("user", "");
		request.getSession().removeAttribute("user");
		return SUCCESS;
	}

	public String userChangePwd() {
		return SUCCESS;
	}

	public String changePwd() {
		User user2 = us.getEntity(user.getUserId());

		user2.setUserPassword(MD5.GetMD5Code(user.getUserPassword()));
		us.updateEntity(user2);
		request.getSession().setAttribute("user", user2);

		return SUCCESS;
	}

	public String userLogin() {
		String hql = "from User u where u.userName=? and u.userPassword=?";
		userList = us.findEntityByHQL(hql, user.getUserName(),
				MD5.GetMD5Code(user.getUserPassword()));
		if (ValidateUtil.isValid(userList)) {
			user = userList.get(0);
			deptList = deptService.findEntityByHQL("from Dept where deptId=?",
					user.getDepartId());
			request.getSession().setAttribute("user", user);
			return SUCCESS;
		} else {
			message = "亲，用户名或密码不正确哦！";
			return "wrong";
		}

	}

	public String userEditUser() {
		User u = us.getEntity(user.getUserId());
		// 用户名
		u.setUserName(user.getUserName());
		// 姓名
		u.setEmpName(user.getEmpName());
		// 性别
		u.setGender(user.getGender());
		// 籍贯
		u.setUserNative(user.getUserNative());
		// 民族
		u.setNation(user.getNation());
		// 备注
		u.setMemo(user.getMemo());

//		 deptList = deptService.findEntityByHQL("from Dept where deptId=?",
//		 user.getDepartId());
		us.updateEntity(u);
		request.getSession().setAttribute("user", u);
		return SUCCESS;
	}

	public String userAdd() {

		user.setUserId(us.getMaxUserId() + 1);
		dept = deptService.getEntity(user.getDepartId());
		user.setDempName(dept.getDeptName());
		if (!StringIsValitedUtils.emailIsValited(user.getUserName())) {
			message = "您输入的email格式不符合规范";
			return "wrong";
		}
		if (user.getUserPassword() == null
				|| "".equals(user.getUserPassword().trim())) {
			message = "密码不可以为空";
			return "wrong";
		} else if (!user.getUserPassword().equals(rePwd)) {
			message = "两次密码不一致";
			return "wrong";
		}
		if (user.getIdcard() == null || "".equals(user.getIdcard().trim())) {
			message = "身份证号不可以为空";
			return "wrong";
		}
		// 判断上传图片的文件名是否为空
		if (ValidateUtil.isValid(pictureFileName)) {
			// 获取上传位置文件夹的服务器路径
			String dir = sc.getRealPath("/upload");
			// 获取当前系统时间作为文件的重命名
			long l = System.nanoTime();
			// 获取上传图片的扩展名
			String ext = pictureFileName.substring(pictureFileName
					.lastIndexOf("."));
			File newFile = new File(dir, l + ext);
			// 上传图片到指定文件
			picture.renameTo(newFile);
			// 设置图片路径保存到服务器
			user.setPicturePath("/upload/" + l + ext);
			// 对密码进行加密
			user.setUserPassword(MD5.GetMD5Code(user.getUserPassword()));
			// 保存用户
			us.saveEntity(user);
			return SUCCESS;
		} else {
			return "wrong";
		}
	}

	public String getAll() {
		userList = us.getAll();
		return SUCCESS;
	}

	public String dropByUserId() {
		us.deleteEntity(user);
		return SUCCESS;
	}

	public String editByUserId() {
		user = us.getEntity(user.getUserId());
		deptList = deptService.findEntityByHQL("from Dept where 1=?", 1);
		return SUCCESS;
	}

	public String getUserChangePath() {
		String hql = "from ChangePath c where c.userId=?";
		changePathList = cps.findEntityByHQL(hql, user.getUserId());
		return SUCCESS;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user
	 *            the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public File getPicture() {
		return picture;
	}

	public void setPicture(File picture) {
		this.picture = picture;
	}

	public String getPictureFileName() {
		return pictureFileName;
	}

	public void setPictureFileName(String pictureFileName) {
		this.pictureFileName = pictureFileName;
	}

	public void setServletContext(ServletContext arg0) {
		this.sc = arg0;

	}

	public List<Dept> getDeptList() {
		return deptList;
	}

	public void setDeptList(List<Dept> deptList) {
		this.deptList = deptList;
	}

	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public List<ChangePath> getChangePathList() {
		return changePathList;
	}

	public void setChangePathList(List<ChangePath> changePathList) {
		this.changePathList = changePathList;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the honerList
	 */
	public List<Honer> getHonerList() {
		return honerList;
	}

	/**
	 * @param honerList
	 *            the honerList to set
	 */
	public void setHonerList(List<Honer> honerList) {
		this.honerList = honerList;
	}

	/**
	 * @return the rePwd
	 */
	public String getRePwd() {
		return rePwd;
	}

	/**
	 * @param rePwd
	 *            the rePwd to set
	 */
	public void setRePwd(String rePwd) {
		this.rePwd = rePwd;
	}

}
